package com.xd;

import cn.hutool.json.JSONUtil;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.ClassPathResource;
import org.springframework.security.jwt.Jwt;
import org.springframework.security.jwt.JwtHelper;
import org.springframework.security.jwt.crypto.sign.RsaSigner;
import org.springframework.security.jwt.crypto.sign.RsaVerifier;
import org.springframework.security.oauth2.provider.token.store.KeyStoreKeyFactory;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.UnsupportedEncodingException;
import java.security.KeyPair;
import java.security.PublicKey;
import java.security.interfaces.RSAPrivateCrtKey;
import java.util.HashMap;
import java.util.Map;

/**
 * @author huboxin
 * @title: test
 * @projectName bk
 * @description:
 * @date 2019/12/3116:31
 */

@RunWith(SpringJUnit4ClassRunner.class)
public class test {

    /*下边命令生成密钥证书，采用RSA 算法每个证书包含公钥和私钥
            keytool -genkeypair -alias xd -keyalg RSA -keypass 123456 -keystore jwt.jks -storepass 123456
            Keytool 是一个java提供的证书管理工具
            -alias：密钥的别名
            -keyalg：使用的hash算法
            -keypass：密钥的访问密码
            -keystore：密钥库文件名，xc.keystore保存了生成的证书
            -storepass：密钥库的访问密码
            查询证书信息：
            keytool -list -keystore xc.keystore
            删除别名
            keytool -delete -alias xckey -keystore xc.keystore*/


    @Test
    public void contextLoads() {

        //密钥库文件
        String keystore="jwt.jks";
        //密钥库密码
        String key_password="123456";

        //密钥别名
        String alias="xd";

        //访问密码
        String password="123456";
        //文件路径
        ClassPathResource classPathResource = new ClassPathResource(keystore);
        //密钥工厂
        KeyStoreKeyFactory Factory = new KeyStoreKeyFactory(classPathResource, key_password.toCharArray());
        //密钥对(公钥和私钥)
        KeyPair keyPair = Factory.getKeyPair(alias, password.toCharArray());
        //获取私钥
        RSAPrivateCrtKey aPrivate = (RSAPrivateCrtKey) keyPair.getPrivate();
        PublicKey aPublic = keyPair.getPublic();

        Map objectObjectMap = new HashMap();
        objectObjectMap.put("name","你好");
        objectObjectMap.put("sex",12);
        objectObjectMap.put("4563sdf sssdf ",12);
        //生成JWT令牌
        Jwt jwt = JwtHelper.encode(JSONUtil.toJsonStr(objectObjectMap), new RsaSigner(aPrivate));

        String encoded = jwt.getEncoded();

        System.out.println(encoded);
    }

    @Test
    public void textverify(){

        //公钥
        String publickey="-----BEGIN PUBLIC KEY-----\n" +
                "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnsnOmrXh4ht65Cule050\n" +
                "e7hQuMeV/6lR24XaX3vpYzEMt67quYXxFxK0yfUc0oji9JURZplpQSib0WXEzsCs\n" +
                "iBiax1ERO7/TP2meKgibbOJhz1zYPmzH6AcvEdSDJXFuHfYtSMN6LQKWzytI2Z5L\n" +
                "BZSsrTeSIrXtwrg4uZUAARuGkvLbgtmR4YxeHUJrQI4/JMQLkPcdrXUOAzJV05ye\n" +
                "tZsncslCjh0Wjt9nhio+kgN+e27BKwhVH8QlVHxPaDIXP5sUrILR5/bUq+LTOX9m\n" +
                "6atYj4HR0uVhf6wpYV+EapIJjmkMgN3njpuj/UTWWxYKfba4RHSkr3hgJ9CeAKQY\n" +
                "rwIDAQAB\n" +
                "-----END PUBLIC KEY-----";
        //token
        String token="eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyI0NTYzc2RmIHNzc2RmICI6MTIsInNleCI6MTIsIm5hbWUiOiLkvaDlpb0ifQ.J58DaAHux9l2MKX8Jp-j05XWxbDwV3t7bcOVSVoui4NkA2LXM9pN1Zh8yKZulcs7fMsZxdFH_5r8FrLjrtgOwenVqsXehoOfgOGl8ISpVxd8pDHeKe72o35TTnLhEJQvqIuMN9I6cmAgJ_N-Kclp8uYQ5MjCENiq_7i_Rhv8K4USz19q5C4VL47DWd3IDlqLj8vtGOV2cJuYyMakXSAqkL3C2N7nhjmusjKdCAb1FMHi0RtYt5VyQzl4KdVSRV7uRMgfGPve6pxCguAoeDxGjBZR92yt2Mh8cEHOJHCxeDj-VlTq_B_C96dIuViFVGoAUpSnz0IWnoAzi9W8d40MlA";
        //这一步校验
        Jwt jwt = JwtHelper.decodeAndVerify(token, new RsaVerifier(publickey));
        String claims = jwt.getClaims();
        System.out.println(claims);

    }


}
